package com.telecomyt.jwportal.webview;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.base.BasePresenter;

import butterknife.BindView;

/**
 * @date: 2020/8/26
 * @author: chengDa
 * @description: webview
 */
public class WebViewActivity extends BaseActivity {

    @BindView(R.id.mWebView)
    WebView mWebView;

    public static void start(Context context) {
        Intent starter = new Intent(context, WebViewActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initView() {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.setWebViewClient(new WebViewClient() {

            @Override

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);

                return true;

            }

        });
        mWebView.addJavascriptInterface(new PlayInterface(), "android");
        mWebView.loadUrl("file:///android_asset/index.html");
    }

    private class PlayInterface {
        @JavascriptInterface
        public void play(String goodsId, String title) {
            /**
             * (1)在manifest配置文件中配置了scheme参数
             * (2)网络端获取url
             * (3)跳转
             */
            String url = "scheme://mtime/goodsDetail?goodsId=" + goodsId + title;
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(url));
            startActivity(intent);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected BasePresenter creatPresenter() {
        return null;
    }


}